/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bsc.util.srping;

import java.util.Locale;
import org.springframework.context.ApplicationContext;

/**
 *
 * @author penn
 *
 * this is util for spring get bean;
 *
 */
public class SpringUtil {

    private static SpringUtil springUtil;
    private ApplicationContext ac;

    private SpringUtil() {
        this.ac = null;
    }

//    synchronized method get class
    public synchronized static SpringUtil getInstance() {
        if (springUtil == null) {
            springUtil = new SpringUtil();
        }

        return springUtil;
    }

//    get bean for name
    public Object getBean(String name) {
        return ac != null ? ac.getBean(name) : null;
    }

    public String getMessage(String key, Object[] obj) {
        return ac != null ? ac.getMessage(key, obj, Locale.ENGLISH) : null;
    }

    public long setAC(ApplicationContext ac) {
        return this.ac != null ? this.ac.getStartupDate() : (this.ac = ac).getStartupDate();
    }

    public void showBean() {
        String[] names = ac.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println("bean name: " + name);
        }
    }
}
